package com.ruoyi.web.controller.lingshui.person;


import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.BasePostProPersonCert;
import com.ruoyi.supervise.domain.BaseSpecialEquPersonCert;
import com.ruoyi.supervise.domain.Enterprise;
import com.ruoyi.supervise.domain.IGMPersonCreditRank;
import com.ruoyi.supervise.domain.IGMPersonalPoorInfo;
import com.ruoyi.supervise.domain.IGMPersonalWellInfo;
import com.ruoyi.supervise.domain.IntegrityPerformance;
import com.ruoyi.supervise.domain.PersonInfo;
import com.ruoyi.supervise.domain.ProTitlePersonCert;
import com.ruoyi.supervise.domain.RARegInfo;
import com.ruoyi.supervise.service.IEnterpriseService;
import com.ruoyi.supervise.service.IPersonInfoService;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: EnterpriseController
 * @Description: 人员controller
 * @Author wtj
 * @DateTime 2020年3月19日 上午10:08:07
 */
@Controller
@RequestMapping("/enterprise/person")
public class PersonController extends BaseController{

	private String prefix = "lingshui/person";
	
	@Autowired
	private IPersonInfoService personInfoService;
	
	@Autowired
    private IEnterpriseService enterpriseService;
	
   /**
    * @Title: detail
    * @Description: 人员详情(基本信息，人员资格，诚信评价)
    * @Author wtj
    * @DateTime 2020年4月23日 下午2:29:09
    * @param guid
    * @param mmap
    * @return
    */
   @RequiresPermissions(value={"person:personall:basicinfo", "person:personall:qua", "person:personall:yjlist", "integrityInformation:personGoodBehavior:list", "integrityInformation:personBadBehavior:list", "person:personall:cxpj"}, logical=Logical.OR)
   @GetMapping("/detail/{guid}")
   public String detail(@PathVariable("guid") String guid,ModelMap mmap)
   {
	   mmap.put("peopleGuid", guid);
	   PersonInfo personInfo = personInfoService.selectPersonInfoByGuid(guid);
	   if(personInfo!=null) {
		   mmap.put("peopleIDCard", personInfo.getIDCard());
		   List<Enterprise> enterList = new ArrayList<Enterprise>();
		   if(!StringUtils.isEmpty(personInfo.getCompanyGuid())) {
			   String[] subStr=StrUtil.split(personInfo.getCompanyGuid(), "|");
			   for(String s:subStr) {
				   Enterprise enterprise = enterpriseService.selectEnterpriseInfoByGuid(s);
				   enterList.add(enterprise);
			   }
		   }
		   mmap.put("enterList", enterList);
		   if(!StringUtils.isEmpty(personInfo.getPersonPhoto())) {
			   String[] subStr=StrUtil.split(personInfo.getPersonPhoto(), "|");
			   if(subStr.length >= 2) {
				   personInfo.setPersonPhoto(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
			   }
		   }
		   if(!StringUtils.isEmpty(personInfo.getIDCardImage())) {
			   String[] subStr=StrUtil.split(personInfo.getIDCardImage(), "|");
			   if(subStr.length >= 2) {
				   personInfo.setIDCardImage(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
			   }
		   }
		   if(!StringUtils.isEmpty(personInfo.getPersonType())) {
			   String personType = "";
			   String[] subStr=StrUtil.split(personInfo.getPersonType(), "|");
			   for(String s:subStr) {
				   if(StringUtils.isEmpty(personType)) {
					   personType = s;
				   }else {
					   personType = personType+ " " + s;
				   }
			   }
			   personInfo.setPersonType(personType);
		   }
		   //注册师信息集合
		   List<RARegInfo> riList = personInfoService.selectBaseRARegInfoByPersonBaseGuid(guid);
		   mmap.put("riList", riList);
		   //专业技术证集合
		   List<ProTitlePersonCert> ptpcList = personInfoService.selectBaseProTitlePersonCertByPersonBaseGuid(guid);
		   mmap.put("ptpcList", ptpcList);
		   //岗位类专业技术人员证集合
		   List<BasePostProPersonCert> bpppcList = personInfoService.selectBasePostProPersonCertByPersonBaseGuid(guid);
		   mmap.put("bpppcList", bpppcList);
		   //造价员证集合
		   List<BasePostProPersonCert> zjyList = new ArrayList<BasePostProPersonCert>();
		   //监理员证集合
		   List<BasePostProPersonCert> jlyList = new ArrayList<BasePostProPersonCert>();
		   //安全生产考核合格证集合
		   List<BasePostProPersonCert> aqList = new ArrayList<BasePostProPersonCert>();
		   //管理岗位证证集合
		   List<BasePostProPersonCert> glList = new ArrayList<BasePostProPersonCert>();
		   if(!StringUtils.isEmpty(bpppcList)) {
			   for(BasePostProPersonCert bpppc:bpppcList) {
				   if(bpppc.getPostType().equals("造价员")) {
					   zjyList.add(bpppc);
				   }else if(bpppc.getPostType().equals("监理员") || bpppc.getPostType().equals("专业监理工程师")) {
					   jlyList.add(bpppc);
				   }else if(bpppc.getPostType().equals("安全生产考核合格") || bpppc.getPostType().equals("企业主要负责人")
						   || bpppc.getPostType().equals("项目负责人") || bpppc.getPostType().equals("机械类专职安全生产管理人员C1")
						   || bpppc.getPostType().equals("土建类专职安全生产管理人员C2") || bpppc.getPostType().equals("综合类专职安全生产管理人员C3")) {
					   aqList.add(bpppc);
				   }else {
					   glList.add(bpppc);
				   }
			   }
		   }
		   mmap.put("zjyList", zjyList);
		   mmap.put("jlyList", jlyList);
		   mmap.put("aqList", aqList);
		   mmap.put("glList", glList);
		   //特种作业岗位证集合
		   List<BaseSpecialEquPersonCert> bsepcList = personInfoService.selectBaseSpecialEquPersonCertByPersonBaseGuid(guid);
		   mmap.put("bsepcList", bsepcList);
		   //特种作业证集合
		   List<BaseSpecialEquPersonCert> tzList = new ArrayList<BaseSpecialEquPersonCert>();
		   //普通岗位证集合
		   List<BaseSpecialEquPersonCert> ptList = new ArrayList<BaseSpecialEquPersonCert>();
		   if(!StringUtils.isEmpty(bsepcList)) {
			   for(BaseSpecialEquPersonCert bsepc:bsepcList) {
				   if(bsepc.getWorkCategory().equals("特种作业")) {
					   tzList.add(bsepc);
				   }else {
					   ptList.add(bsepc);
				   }
			   }
		   }
		   mmap.put("tzList", tzList);
		   mmap.put("ptList", ptList);
		   if(!StringUtils.isEmpty(ptpcList)) {
			   String proTechQualStr = "";
			   for(ProTitlePersonCert ptpc:ptpcList) {
				   if(StringUtils.isEmpty(proTechQualStr)) {
					   proTechQualStr = ptpc.getQualName();
				   }else {
					   proTechQualStr = proTechQualStr+" ,"+ptpc.getQualName();
				   }
			   }
			   personInfo.setProTechQual(proTechQualStr);
		   }
		   if(!StringUtils.isEmpty(riList)) {
			   String qualicationStr = "";
			   for(RARegInfo ri:riList) {
				   if(StringUtils.isEmpty(qualicationStr)) {
					   qualicationStr = ri.getRegType();
				   }else {
					   qualicationStr = qualicationStr+ri.getRegType();
				   }
			   }
			   personInfo.setQualication(qualicationStr);
		   }
		   mmap.put("personInfo", personInfo);
	   }
	   //人员诚信评价
	   List<IGMPersonCreditRank> igmpcrList = personInfoService.selectIGMPersonCreditRankByPersonGuid(guid);
	   mmap.put("igmpcrList", igmpcrList);
	   if(!StringUtils.isEmpty(igmpcrList)) {
		   mmap.put("igmpcrListOne", igmpcrList.get(0));
	   }else {
		   mmap.put("igmpcrListOne", null);
	   }
	   return prefix + "/personDetail";
   }
   
   /**
    * @Title: renyuanyejixinxiallList
    * @Description: 获取人员业绩信息列表数据
    * @Author wtj
    * @DateTime 2020-9-29 16:10:33
    * @param peopleIDCard
    * @param ip
    * @return
    */
   @RequiresPermissions("person:personall:yjlist")
   @PostMapping("/renyuanyejixinxiallList/{peopleIDCard}")
   @ResponseBody
   public TableDataInfo renyuanyejixinxiallList(@PathVariable("peopleIDCard") String peopleIDCard,IntegrityPerformance ip)
   {
	   startPage();
	   if(!StringUtils.isEmpty(peopleIDCard)) {
		   ip.setIdCard(peopleIDCard);
	   }
	   List<IntegrityPerformance> ipList = personInfoService.selectIntegrityPerformanceListByPeopleIdCard(ip);
	   return getDataTable(ipList);
   }
   
   /**
    * @Title: renyuanlianghaoList
    * @Description: 人员良好行为获取列表数据
    * @Author wtj
    * @DateTime 2020-9-27 15:08:36
    * @param peopleGuid
    * @param igmpwi
    * @return
    */
   @RequiresPermissions("integrityInformation:personGoodBehavior:list")
   @PostMapping("/renyuanlianghaoList/{peopleGuid}")
   @ResponseBody
   public TableDataInfo renyuanlianghaoList(@PathVariable("peopleGuid") String peopleGuid,IGMPersonalWellInfo igmpwi)
   {
	   startPage();
	   if(!StringUtils.isEmpty(peopleGuid)) {
		   igmpwi.setPersonGuid(peopleGuid);
	   }
	   List<IGMPersonalWellInfo> igmpwiList = personInfoService.selectIGMPersonalWellInfoListByPersonGuid(igmpwi);
	   return getDataTable(igmpwiList);
   }
   
   /**
    * @Title: renyuanlianghaoDetail
    * @Description: 人员良好行为详情
    * @Author wtj
    * @DateTime 2020-9-27 15:09:23
    * @param guid
    * @param mmap
    * @return
    */
   @RequiresPermissions("integrityInformation:personGoodBehavior:detail")
   @GetMapping("/renyuanlianghaoDetail/{guid}")
   public String renyuanlianghaoDetail(@PathVariable("guid") String guid,ModelMap mmap)
   {
	   IGMPersonalWellInfo igmpwi = personInfoService.selectIGMPersonalWellInfoByGuid(guid);
	   mmap.put("igmpwi", igmpwi);
	   return prefix+"/lianghaoxingwei";
   }
   
   /**
    * @Title: renyuanbuliangList
    * @Description: 人员不良行为获取列表数据
    * @Author wtj
    * @DateTime 2020-9-27 15:09:48
    * @param peopleGuid
    * @param igmppi
    * @return
    */
   @RequiresPermissions("integrityInformation:personBadBehavior:list")
   @PostMapping("/renyuanbuliangList/{peopleGuid}")
   @ResponseBody
   public TableDataInfo renyuanbuliangList(@PathVariable("peopleGuid") String peopleGuid,IGMPersonalPoorInfo igmppi)
   {
	   startPage();
	   if(!StringUtils.isEmpty(peopleGuid)) {
		   igmppi.setPersonGuid(peopleGuid);
	   }
	   List<IGMPersonalPoorInfo> igmppiList = personInfoService.selectIGMPersonalPoorInfoListByPersonGuid(igmppi);
	   return getDataTable(igmppiList);
   }
   
   /**
    * @Title: renyuanbuliangDetail
    * @Description: 人员不良行为详情
    * @Author wtj
    * @DateTime 2020-9-27 15:10:27
    * @param guid
    * @param mmap
    * @return
    */
   @RequiresPermissions("integrityInformation:personBadBehavior:detail")
   @GetMapping("/renyuanbuliangDetail/{guid}")
   public String renyuanbuliangDetail(@PathVariable("guid") String guid,ModelMap mmap)
   {
	   IGMPersonalPoorInfo igmppi = personInfoService.selectIGMPersonalPoorInfoByGuid(guid);
	   mmap.put("igmppi", igmppi);
	   return prefix+"/buliangjilu";
   }
}
